package com.kkb.kkbcommon.basic;

import io.lettuce.core.Consumer;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: zml
 * @Description: 正则表达式工具类
 * @Data Created in 2021-08-05 20:39
 * @Modified by:
 */
public class RegexUtil {

    /**
     * @Author: zml
     * @Description: 根据传入的字符串和正则表达式查看是否匹配
     * @Data Created in 2021-08-05 20:41:32
     * @Modified by:
     *
     * @param str 要匹配的字符串
     * @param regex 正则表达式
     * @return 如果匹配成功返回true，否则返回false
     */
    public static boolean isMatchByRegex(String str, String regex){
        if (BaseUtil.haveEmpty(str, regex)) {return false;}
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    /**
     * @Author: zml
     * @Description: 根据传入的字符串和正则表达式获取匹配器
     * @Data Created in 2021-07-13 14:52:18
     * @Modified by:
     *
     * @param str 要匹配的字符串
     * @param regex 正则表达式
     * @return 如果匹配成功返回匹配器，否则返回null
     */
    public static Matcher getMatcherByRegex(String str, String regex){
        if (BaseUtil.haveEmpty(str, regex)) {return null;}
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        boolean b = matcher.find();
        return b?matcher:null;
    }

}
